package com.common.interceptor;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PoliceInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        // 过滤ajax请求，每个ajax请求都携带自己菜单的id，如果对应的id能够查询到，则成功，否则失败。
        // 此过滤方案在地址栏中如果直接输入ajax请求地址依然能得到结果集，所以抛弃之。
        // if (request.getHeader("x-requested-with") != null
        // &&
        // request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest"))
        // {
        //
        // return false;
        // }
        // ControllerName将对应着数据库菜单，菜单的ControllerName名称。
        if (handler instanceof HandlerMethod) {
            HandlerMethod hm = (HandlerMethod) handler;
        }
        return true;
    }

    /* (non-Javadoc)
     * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
     * Controller完成后执行的方法。
     *
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod hm = (HandlerMethod) handler;
        }

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        if(handler instanceof HandlerMethod){
            HandlerMethod hm = (HandlerMethod)handler;
            //对所有的需要防表单重复提交的页面做不缓存操作。
            //也就是对所有的编辑页面做不缓存操作。
//            SubToken st = hm.getMethodAnnotation(SubToken.class);
//            if(st!=null)
//                if(st.needSaveToken()){
//                    response.setHeader("Cache-Control", "no-cache");
//                    response.setHeader("Cache-Control", "no-store");
//                    response.setHeader("Pragma", "no-cache");
//                    response.setDateHeader("Expires", 0);
//                }
        }
    }

}
